232. Implement Queue using Stacks
Implement the following operations of a queue using stacks.
push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the front element. empty() -- Return whether the queue is empty.
class MyQueue {
ArrayDeque< Integer> queue = new ArrayDeque<>();
/** Push element x to the back of queue. */
public void push(int x) {
queue.offer(x);
}
/** Removes the element from in front of queue and returns that element. */
public int pop() {
int e = queue.peek();
queue.poll();
return e;
}
/** Get the front element. */
public int peek() {
return queue.peek();
}
/** Returns whether the queue is empty. */
public boolean empty() {
return queue.isEmpty();
}
}
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* boolean param_4 = obj.empty();
*/